<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class admin_coordinator extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->model('model_coordinator', 'm_coor', TRUE);
        $this->load->model('model_state', 'm_state', TRUE);
    }
    
    function index($offset = 0, $order_column = 'datecreated', $order_type = 'DESC') {
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        else{
            
            $data = array(
                'web_title' =>  'Admin coordinator',
                'main_view' =>  'mimin/coordinator/index_new',
                'list_data' =>  $this->m_coor->get_all(),
            );
            $this->load->view('mimin/template/template_table',$data);
        }
    }
    function add(){
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        elseif($this->session->userdata('admin_user')->level != 1){
            redirect("admin_home","refresh");
        }
        else{
            $this->form_validation->set_rules('email','email','required');
            $this->form_validation->set_rules('name','Nama','required');
            $this->form_validation->set_rules('password','Password','required');
            if($this->form_validation->run() == FALSE){
                // set common properties
                $data = array(
                    'web_title'     =>  'Data Anggota Baru',
                    'main_view'     =>  "mimin/coordinator/add",
                    "list_state"    =>  $this->m_state->get_all(),
                );
                $this->parser->parse("mimin/template/template_editor",$data);
            }
            else{
                extract($_POST);
                $count = $this->m_coor->count_by_email($email);
                if($count > 1){
                    $output = array(
                        'status'    =>  0,
                        'msg'       =>  "<b>".$email."</b> already registered",
                    );
                    echo json_encode($output);exit();
                }
                else{
                    //SET TIME ZONE
                    date_default_timezone_set("Asia/Jakarta");
                    $curdate = date("Y-m-d h:i:s");
                    
                    $isi_new = array(
                        'email' =>  $email,
                        'name'  =>  $name,
                        'password'  =>  md5("12345"),
                        'datecreated'  =>  $curdate,
                    );
                }
            }
        }
    }
    function add_act(){
        $this->form_validation->set_rules('email','email','required');
        $this->form_validation->set_rules('name','Nama','required');
        $this->form_validation->set_rules('province','Nama','required');
        if($this->form_validation->run() == FALSE){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "All fields must be filled",
            );
            echo json_encode($output);exit();
        }
        else{
            extract($_POST);
            $count = $this->m_coor->count_by_email($email);
            if($count > 0){
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  " <i>".$email."</i> already registered",
                );
                echo json_encode($output);exit();
            }
            else{
                //SET TIME ZONE
                date_default_timezone_set("Asia/Jakarta");
                $curdate = date("Y-m-d h:i:s");

                $isi_new = array(
                    'username'      =>  $email,
                    'name'          =>  $name,
                    'password'      =>  md5("12345"),
                    'state_id'      =>  $province,
                    'datecreated'   =>  $curdate,
                );
                $status_save = $this->m_coor->save_data($isi_new);
                if($status_save){
                    $output = array(
                        'status'    =>  1,
                        'msg'       =>  " New Data has been inserted",
                    );
                    echo json_encode($output);exit();
                }
                else{
                    $output = array(
                        'status'    =>  0,
                        'msg'       =>  "Save data failed, ERROR : ".  mysql_error(),
                    );
                    echo json_encode($output);exit();
                }
            }
        }
    }
    function delete(){
        if(!$this->session->userdata('admin_user')){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You need to re-login"
            );
            echo json_encode($output);exit();
        }
        if($this->session->userdata('admin_user')->level != 1){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You're not allowed to run this function"
            );
            echo json_encode($output);exit();
        }
        $this->form_validation->set_rules('id','id coordinator','required');
        if($this->form_validation->run() == FALSE){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "ID Coordinator is missed"
            );
            echo json_encode($output);exit();
        }
        else{
            extract($_POST);
            //SET TIME ZONE
            date_default_timezone_set("Asia/Jakarta");
            $curdate = date("Y-m-d h:i:s");
            $count = $this->m_coor->select_by_id($id,"count");
            if($count > 0){
                $list_data = $this->m_coor->select_by_id($id,"result");
                $status_deleted = "";
                foreach($list_data as $r):
                    $status_deleted = $r->deleted;
                endforeach;
                
                $this->db->trans_begin();
                
                if($status_deleted != 1){
                    $update_data = array(
                        'deleted'    =>  1
                    );
                    $status_update = $this->m_coor->update_data($id,$update_data);
                    if($status_update){
                        if ($this->db->trans_status() === FALSE)
                        {
                            $this->db->trans_rollback();
                            $output = array(
                                'status'    =>  0,
                                'msg'       =>  "Data deletion is failed, ERROR : ".  mysql_error()
                            );
                            echo json_encode($output);exit();
                        }
                        else
                        {
                            $this->db->trans_commit();
                            $output = array(
                                'status'    =>  1,
                                'msg'       =>  "Succesfully Change data status to deleted"
                            );
                            echo json_encode($output);exit();
                        }
                    }
                    else{
                        $this->db->trans_rollback();
                        $output = array(
                            'status'    =>  0,
                            'msg'       =>  "Delete coordinator data unsuccessful, ERROR : ".  mysql_error()
                        );
                        echo json_encode($output);exit();
                    }
                }
                else{
                    //delete permanently

                    $status_delete = $this->m_coor->delete($id);
                    if($status_delete){
                        if ($this->db->trans_status() === FALSE)
                        {
                            $this->db->trans_rollback();
                            $output = array(
                                'status'    =>  0,
                                'msg'       =>  "Delete coordinator data permanently unsuccessful"
                            );
                            echo json_encode($output);exit();
                        }
                        else
                        {
                            $this->db->trans_commit();
                            $output = array(
                                'status'    =>  1,
                                'msg'       =>  "success Delete coordinator data permanently"
                            );
                            echo json_encode($output);exit();
                        }
                    }
                    else{
                        $this->db->trans_rollback();
                        $output = array(
                            'status'    =>  0,
                            'msg'       =>  "Delete coordinator data permanently unsuccessful, ERROR : ".  mysql_error()
                        );
                        echo json_encode($output);exit();
                    }
                }
            }
            else{
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "Data is missed, you should refresh the page"
                );
                echo json_encode($output);exit();
            }
        }
    }
    function undelete(){
        if(!$this->session->userdata('admin_user')){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You need to re-login"
            );
            echo json_encode($output);exit();
        }
        if($this->session->userdata('admin_user')->level != 1){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You're not allowed to run this function"
            );
            echo json_encode($output);exit();
        }
        $this->form_validation->set_rules('id','id coordinator','required');
        if($this->form_validation->run() == FALSE){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "ID Member is missed"
            );
            echo json_encode($output);exit();
        }
        else{
            extract($_POST);
            $count = $this->m_coor->select_by_id($id,"count");
            if($count > 0){
                $this->db->trans_begin();
                $update_data = array(
                    'deleted'    =>  0
                );
                $status_update = $this->m_coor->update_data($id,$update_data);
                if($status_update){
                    if ($this->db->trans_status() === FALSE)
                    {
                        $this->db->trans_rollback();
                        $output = array(
                            'status'    =>  0,
                            'msg'       =>  "Data restoration is failed, ERROR : ".  mysql_error()
                        );
                        echo json_encode($output);exit();
                    }
                    else
                    {
                        $this->db->trans_commit();
                        $output = array(
                            'status'    =>  1,
                            'msg'       =>  "Succesfully restore data to deleted"
                        );
                        echo json_encode($output);exit();
                    }
                }
                else{
                    $this->db->trans_rollback();
                    $output = array(
                        'status'    =>  0,
                        'msg'       =>  "restoration data unsuccessful, ERROR : ".  mysql_error()
                    );
                    echo json_encode($output);exit();
                }
            }
            else{
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "Data is missed, you should refresh the page"
                );
                echo json_encode($output);exit();
            }
        }
    }
    function block(){
        if(!$this->session->userdata('admin_user')){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You need to re-login"
            );
            echo json_encode($output);exit();
        }
        if($this->session->userdata('admin_user')->level != 1){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You're not allowed to run this function"
            );
            echo json_encode($output);exit();
        }
        $this->form_validation->set_rules('id','id coordinator','required');
        if($this->form_validation->run() == FALSE){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "ID Member is missed"
            );
            echo json_encode($output);exit();
        }
        else{
            extract($_POST);
            $count  = $this->m_coor->select_by_id($id,"count");
            if($count > 0){
                $data   = $this->m_coor->select_by_id($id);
                $username="";
                foreach ($data as $r) {
                    $username = $r->username;
                }
                $this->db->trans_begin();
                $update_data = array(
                    'status'    =>  0
                );
                $status_update = $this->m_coor->update_data($id,$update_data);
                if($status_update){
                    if ($this->db->trans_status() === FALSE)
                    {
                        $this->db->trans_rollback();
                        $output = array(
                            'status'    =>  0,
                            'msg'       =>  "Blocking coordinator is failed, ERROR : ".  mysql_error()
                        );
                        echo json_encode($output);exit();
                    }
                    else
                    {
                        $this->db->trans_commit();
                        $output = array(
                            'status'    =>  1,
                            'msg'       =>  "Succesfully block coordinator ".$username
                        );
                        echo json_encode($output);exit();
                    }
                }
                else{
                    $this->db->trans_rollback();
                    $output = array(
                        'status'    =>  0,
                        'msg'       =>  "Blocking data unsuccessful, ERROR : ".  mysql_error()
                    );
                    echo json_encode($output);exit();
                }
            }
            else{
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "Data is missed, you should refresh the page"
                );
                echo json_encode($output);exit();
            }
        }
    }
    function unblock(){
        if(!$this->session->userdata('admin_user')){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You need to re-login"
            );
            echo json_encode($output);exit();
        }
        if($this->session->userdata('admin_user')->level != 1){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "You're not allowed to run this function"
            );
            echo json_encode($output);exit();
        }
        $this->form_validation->set_rules('id','id coordinator','required');
        if($this->form_validation->run() == FALSE){
            $output = array(
                'status'    =>  0,
                'msg'       =>  "ID Member is missed"
            );
            echo json_encode($output);exit();
        }
        else{
            extract($_POST);
            $count  = $this->m_coor->select_by_id($id,"count");
            if($count > 0){
                $data   = $this->m_coor->select_by_id($id);
                $email="";
                foreach ($data as $r) {
                    $email = $r->username;
                }
                $this->db->trans_begin();
                $update_data = array(
                    'status'    =>  1
                );
                $status_update = $this->m_coor->update_data($id,$update_data);
                if($status_update){
                    if ($this->db->trans_status() === FALSE)
                    {
                        $this->db->trans_rollback();
                        $output = array(
                            'status'    =>  0,
                            'msg'       =>  "Unlocking coordinator is failed, ERROR : ".  mysql_error()
                        );
                        echo json_encode($output);exit();
                    }
                    else
                    {
                        $this->db->trans_commit();
                        $output = array(
                            'status'    =>  1,
                            'msg'       =>  "Succesfully unblock coordinator ".$email
                        );
                        echo json_encode($output);exit();
                    }
                }
                else{
                    $this->db->trans_rollback();
                    $output = array(
                        'status'    =>  0,
                        'msg'       =>  "Unblocking data unsuccessful, ERROR : ".  mysql_error()
                    );
                    echo json_encode($output);exit();
                }
            }
            else{
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "Data is missed, you should refresh the page"
                );
                echo json_encode($output);exit();
            }
        }
    }
    
    
    function detail($id=""){
        //SET TIME ZONE
        date_default_timezone_set("Asia/Jakarta");
        $curdate = date("Y-m-d h:i:s");
        if(!$this->session->userdata('admin_user')){
            redirect("admin_ippi","refresh");
        }
        elseif($this->session->userdata('admin_user')->level != 1){
            redirect("admin_home","refresh");
        }
        elseif($id==""){
            redirect("admin_coordinator","refresh");
        }
        else{
            $count = $this->m_coor->select_by_id($id,"count");
            if($count > 0){
                $data = array(
                    "main_view"     =>  "mimin/coordinator/detail",
                    "web_title"     =>  "Detail coordinator Data",
                    "list_data"     =>  $this->m_coor->select_by_id($id),
                    "list_state"    =>  $this->m_state->get_all(),
                );
                $this->load->view("mimin/template/template_editor",$data);
            }
            else{
                redirect("admin_news","refresh");
            }
        }
    }
    function detail_act(){
        if(!$this->session->userdata('admin_user')){
            $output = array(
                'status'    => 0,
                'msg'       => "You need to re-login the system",
            );
            echo json_encode($output);exit();
        }
        elseif($this->session->userdata('admin_user')->level != 1){
            $output = array(
                'status'    => 0,
                'msg'       => "You have no access level",
            );
            echo json_encode($output);exit();
        }
        else{
            $this->form_validation->set_rules('id','id','required');
            $this->form_validation->set_rules('name','Nama','required');
            $this->form_validation->set_rules('province','provinsi','required');
            $this->form_validation->set_rules('status','status','required');
            if($this->form_validation->run() == FALSE){
                $output = array(
                    'status'    =>  0,
                    'msg'       =>  "All fields must be filled",
                );
                echo json_encode($output);exit();
            }
            else{
                extract($_POST);
                $count = $this->m_coor->select_by_id($id,"count");
                if($count > 0){
                    //cek state_id
                    $count = $this->m_state->select_by_id($id,"count");
                    if($count == 0){
                        $output = array(
                            'status'    =>  0,
                            'msg'       =>  "Cannot find Province id in database",
                        );
                        echo json_encode($output);exit();
                    }
                    //SET TIME ZONE
                    date_default_timezone_set("Asia/Jakarta");
                    $curdate = date("Y-m-d h:i:s");
                    
                    $isi_update = array(
                        'name'          =>  $name,
                        'status'        =>  $status,
                        'state_id'      =>  $province,
                        'datemodified'  =>  $curdate,
                    );
                    $status_update = $this->m_coor->update_data($id,$isi_update);
                    if($status_update){
                        $output = array(
                            'status'    =>  1,
                            'msg'       =>  "Data has been updated",
                        );
                        echo json_encode($output);exit();
                    }
                    else{
                        $output = array(
                            'status'    =>  0,
                            'msg'       =>  "Failed update data",
                        );
                        echo json_encode($output);exit();
                    }
                }
                else{
                    $output = array(
                        'status'    =>  0,
                        'msg'       =>  " The data might be deleted, please refresh the page",
                    );
                    echo json_encode($output);exit();
                }
            }
        }
    }
}

/* End of file admin_coordinator.php */
/* Location: ./application/controllers/admin_coordinator.php */